# -*- coding: utf-8 -*-
# @Time    : 2018-05-29 14:57
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : Rule18_AdminPenalty_3Yr.py
# @Software: PyCharm


'''eof
name:3年内行政处罚校验
code:AdminPenalty_3Yr
tableName:
columnName:
groups:准入>个人征信
dependencies:IC
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import json
import datetime
from dateutil.relativedelta import relativedelta
import re
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', None, 'null', '/', 'Null']

def FormatData(tableName):
    try:
        icr = json.loads(IC[0]["icr"])[tableName]
        return icr if isinstance(icr, list) and len(icr) > 0 else [{}]
    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) if float(dataValue) >= 0 else None
        except:
            returnData = None
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData


def Formate(time):
    r = re.compile(r'\D+')
    try:
        date = datetime.datetime.strptime(time, '%Y.%m.%d').strftime('%Y-%m-%d')
    except:
        try:
            date = datetime.datetime.strptime(time, '%Y-%m-%d').strftime('%Y-%m-%d')
        except:
            try:
                date = datetime.datetime.strptime(time, '%Y/%m/%d').strftime('%Y-%m-%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.datetime.strptime(tmp, '%Y%m%d').strftime('%Y-%m-%d')
                except:
                    date = None
    return date



def AdminPenalty3Yr():
    try:
        # 行政处罚记录
        cirAdminPunishmentList = FormatData("ICRAdminPunishment")
        if cirAdminPunishmentList == [{}]:
            return u'缺失值'
        else:
            returnList= []
            for cirAdminPunishment in cirAdminPunishmentList:
                try:
                    beginDate = cirAdminPunishment.get("BeginDate")
                    money = ConvertDataType(cirAdminPunishment.get("Money"), 'float')
                    if Formate(beginDate) in null_type_list or money in null_type_list:
                        returnList.append(None)
                        continue
                    beginDateFormat = datetime.datetime.strptime(Formate(beginDate), "%Y-%m-%d")
                    today = datetime.datetime.now()
                    threeYearsAgo = today - relativedelta(years=3)
                    if beginDateFormat > today or money in null_type_list:
                        returnList.append(None)
                        continue
                    if beginDateFormat > threeYearsAgo and beginDateFormat <= today \
                            and ConvertDataType(money, 'None') > 1000:
                        returnList.append(1)
                    else:
                        returnList.append(0)
                except:
                    returnList.append(None)
                    continue
            returnListNotNone = filter(lambda x:x is not None, returnList)
            if len(returnListNotNone) == 0:
                return u'缺失值'
            else:
                return sum(returnListNotNone)
    except:
        return u"缺失值"

result = AdminPenalty3Yr()